package com.zbs.framework.pay.util;

import com.alipay.api.internal.util.file.ByteArrayOutputStream;
import com.zbs.framework.pay.qrcode.QRCodeUtil;
import com.zbs.framework.pay.qrcode.QrCodeResponse;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;

public class LogoUtil {

    public static byte[] getPayLogoByte(QrCodeResponse qrCodeResponse){
        try {
            String faviconPath = FaviconUtil.getFaviconPath();
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            BufferedImage buffImg;
            if (StringUtils.hasLength(faviconPath)){
                buffImg = QRCodeUtil.encode(qrCodeResponse.getQr_code(), faviconPath, true);//获取二维码
            } else {
                buffImg= QRCodeUtil.encode(qrCodeResponse.getQr_code(),"",true);
            }
            ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
            ImageIO.write(buffImg, "jpg", imageOut);
            imageOut.close();
            ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
            return FileCopyUtils.copyToByteArray(input);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getPayLogoBase64Image(QrCodeResponse qrCodeResponse){
        return QrUtils.getQRCodeImageBase64(getPayLogoByte(qrCodeResponse));
    }
}
